package com.lcq.study.test.demo.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSON;
import com.lcq.study.test.demo.annotation.AesRequestBody;
import com.lcq.study.test.demo.annotation.AesResponseBody;
import com.lcq.study.test.demo.entity.EncryptPeople;
import com.lcq.study.test.demo.service.HelloService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName HelloController
 * @Description
 * @Author lichuanqi
 * @Date 2022/5/12 11:56
 * @Version 1.0
 **/
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Resource
    private HelloService helloService;

    @RequestMapping("/getName")
    public String getName(String id){
        return helloService.getName(id);
    }

    @PostMapping("/aesParam")
    public EncryptPeople aesParam1(@RequestBody EncryptPeople people){
        people.setAge(people.getAge()+100);
        return people;
    }

    @PostMapping("/aesParam1")
    public EncryptPeople aesParam1(@RequestBody String str){
        String result = decrypt(str);
        EncryptPeople people = JSON.parseObject(result, EncryptPeople.class);
        return people;
    }

    @PostMapping("/aesParam2")
    @AesResponseBody
    public EncryptPeople aesParam2(@AesRequestBody @RequestBody EncryptPeople people){
        people.setAge(people.getAge()+100);
        return people;
    }

    private String decrypt(String encrypt){
        String key = "abcdef0123456789";
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes(StandardCharsets.UTF_8));
        return aes.decryptStr(encrypt, CharsetUtil.CHARSET_UTF_8);
    }



}
